
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    motto: '请稍等\n\n正在识别您的身份',
    userInfo: {},
    multiuser: false,
    multiuserSelected: false,
    auth_result: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    hasUserInfo: false,
    identifyingCode: "",
    mobileInput: "",
    idcodeInput: "",
    idcode: "",
    state: { text: "已发送", type: "primary", pass: false },
    hasName:false,
    auth_result:false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName +'--用户授权',
      path: '/pages/schedule/apps/userAuth'
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +" - 智慧配送管理平台" });
    wx.getSetting({
      success(res) {
        console.log(res.authSetting)
        // res.authSetting = {
        //   "scope.userInfo": true,
        //   "scope.userLocation": true
        // }
        if (res.authSetting["scope.userInfo"] == true) {
          that.setData({
            hasUserInfo: true
          })
        }
        // wx.getUserInfo({
        //   success: function (res) {
        //     that.setData({
        //       userInfo: res.userInfo
        //     })
        //   }
        // });
          
      }
    })
  },
  bindGetUserInfo: function () {
    wx.redirectTo({
      url: '/pages/schedule/apps/userAuth',
    })
  },
  getPhoneNumber: function (e) {
    console.log(e)
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    wx.request({
      url: app.server+'/wechat/passport/getPhoneNumber',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        sessionKey: '',
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      },
      success: function (res) {
        console.log(res);
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },
  rephoneauth: function () {
    wx.redirectTo({
      url: '/pages/auth/phoneauth'
    })
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);

    if (item == 'idcode') {
      if (e.detail.value != this.data.code) {
        this.setData({
          state: { text: "验证码错误", type: "warn", pass: false }
        })
      } else {
        this.setData({
          state: { text: "验证码通过", type: "primary", pass: true }
        })
      };
    }
    if(item == 'name' && String(e.detail.value).length > 1){
       this.setData({
         hasName:true
       })
    }
  },
  getIdentifyingCode: function () {
    var that = this;

    // wx.getSetting({
    //   success(res) {
    //     if (res.authSetting['scope.userInfo']) {
    //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称
    //       wx.getUserInfo({
    //         success: function (res) {
    //           console.log(res.userInfo)
    //         }
    //       })
    //     }
    //   }
    // })

    if (!that.data.phone) {
      wx.showToast({
        title: "请输入手机号码",
        icon: "loading",
        duration: 1000
      });
      return false;
    };
    app.getIdentifyingCode(that.data.phone,false, function (code) {
      that.data.code = code;
      that.setData({
        identifyingCode: true
      });
    });
  },
  getIdentifyingMobile: function (e) {
    var that = this;
    wx.showLoading({
      title: '验证中...',
    });
    try {
      wx.setStorageSync('mobile', that.data.phone)
    } catch (e) {
    }
    if (app.globalData.unionid == undefined) {
      wx.hideLoading();
      wx.showModal({
        title: app.appName,
        content: "请先关注'" + app.appName+"'的公众号。业务信息通知将全部通过公众号推送。",
        showCancel: false,
        success: function (res) {
          if (res.confirm) {

          } else if (res.cancel) {

          }
        }
      });
      return false;
    }
    console.log(app.globalData)
    wx.request({
      url: app.server+'/wechat/passport/userAuth',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        name: that.data.name,
        phone: that.data.phone,
        openid: app.globalData.openid,
        unionid: app.globalData.unionid,
        formId: e.detail.formId
      },
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.success) {
          wx.showToast({
            title: "验证成功",
            icon: "success",
            duration: 2000
          });
          that.setData({
            auth_result: true
          });
        } else {
          that.setData({
            auth_result: false
          });
        }
      },
      fail: function (error) {
        wx.hideLoading();
        console.log(error);
        that.setData({
          auth_result_fail: true
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})